package com.duleme.service.impl;

import com.duleme.common.Config;
import com.duleme.dao.BookDao;
import com.duleme.dao.DeliveryDao;
import com.duleme.dao.OrderDao;
import com.duleme.dao.ShopDao;
import com.duleme.entity.*;
import com.duleme.service.ShopService;

import java.util.ArrayList;
import java.util.List;

public class ShopServiceImpl implements ShopService {
    ShopDao shopDao = new ShopDao();
    OrderDao orderDao = new OrderDao();
    DeliveryDao deliveryDao = new DeliveryDao();

    @Override
    public Order getOrderByID(int orderID) {
        Order order = orderDao.getOrder(orderID);
        if (order.getOrderState()==0){
            return null;
        }else {
            return order;
        }
    }

    @Override
    public List<Order> getOrderList(Shop shop) {
        List<Integer> list = orderDao.getOrderByShop(shop);
        List<Order> orders = new ArrayList<>();
        for (Integer i :list){
            orders.add(orderDao.getOrder(i));
        }
        return orders;
    }

    @Override
    public Boolean setOrderState(Order order) {
        if (order == null){
            return false;
        }else {
            return orderDao.setState(order);
        }
    }

    @Override
    public List<Delivery> getDelivery(Shop shop) {
        List<Order> list = getOrderList(shop);
        List<Delivery> deliveries = new ArrayList<>();
        for (Order o:list){
            deliveries.add(deliveryDao.getDeliveryByID(o.getDeliveryID()));
            System.out.println(o.getShopID());
        }
        return deliveries;
    }

    @Override
    public List<DetaileOrder> getDetaileOrder(Shop shop) {
        List<Delivery> deliveries = getDelivery(shop);
        List<Order> orders =getOrderList(shop);

        List<DetaileOrder> list = new ArrayList<>();
        for (int i= 0;i<orders.size();i++){
            DetaileOrder detaileOrder = new DetaileOrder();
            Delivery delivery = deliveries.get(i);
            Order order = orders.get(i);
            String address = delivery.getCountry()+delivery.getProvince()+delivery.getCity()+delivery.getCounty()+delivery.getStreet()+delivery.getDetailedAddress();
            detaileOrder.setAddress(address);
            detaileOrder.setName(delivery.getName());
            detaileOrder.setOrderState(Config.orderState.get(order.getOrderState()));
            detaileOrder.setOrderId(String.valueOf(order.getOrderID()));
            detaileOrder.setPayTime(order.getCreateTime().toString());
            detaileOrder.setPhoneNum(delivery.getPhoneNumber());
            list.add(detaileOrder);
        }
        return list;
    }

    @Override
    public List<DetaileOrder> getDetaileOrderBystate(Shop shop, int orderState) {
         List<DetaileOrder> allList = getDetaileOrder(shop);
         String state = Config.orderState.get(orderState);
         if (state == null){
             return null;
         }
         else {
             List<DetaileOrder> list = new ArrayList<>();
             for (DetaileOrder order:allList){
                 if (order.getOrderState()==state){
                     list.add(order);
                 }
             }
             return list;
         }
    }

    @Override
    public List<Integer> getOrderIDByUser(int userID, int orderState) {
        Order order = new Order();
        order.setUserID(userID);
        order.setOrderState(orderState);
        return orderDao.getOrderIDbyUser(order);
    }

    @Override
    public Order getOrderByUser(int userID, int shopID, int orderState) {
        Order order = new Order();
        order.setOrderState(orderState);
        order.setShopID(shopID);
        order.setUserID(userID);
        int orderID=orderDao.getOrderIDbyID(order);
        if (orderID>0){
            return orderDao.getOrder(orderID);
        }else {
            return null;
        }
    }

    @Override
    public AllOrder getAllOrder(int userID) {
        Order order = new Order();
        order.setUserID(userID);
        order.setOrderState(Config.placeit);
        List<Integer> orderIDs = orderDao.getOrderIDbyUser(order);
        AllOrder allOrder = new AllOrder();
        if (orderIDs!=null){
            for (Integer i:orderIDs){//i:订单号
                Order o = orderDao.getOrder(i);
                List<Book> books= o.getBooks();
                List<Integer> newBook = new ArrayList<>();
                List<Integer> newList = new ArrayList<>();
                for (Book b:books){
                    if (!newBook.contains(b.getBookId())){
                        newBook.add(b.getBookId());
                        newList.add(1);
                    }else {
                        int index = newList.size()-1;
                        newList.set(index,newList.get(index)+1);
                    }
                }
                List<BookItem> items = new ArrayList<>();
                for (int index=0;index<newBook.size();index++){
                    BookItem bookItem = new BookItem();
                    int bookID = newBook.get(index);
                    int bookNum = newList.get(index);
                    Book book = new BookDao().findBookByBookId(bookID);
                    bookItem.setBookId(bookID);
                    bookItem.setBookName(book.getBookName());
                    bookItem.setBookNum(bookNum);
                    bookItem.setCategory(book.getCategory());
                    bookItem.setPrice(book.getPrice());
                    bookItem.setRemain(book.getRemain());
                    bookItem.setSell(book.getSell());
                    bookItem.setShopId(book.getShopId());
                    bookItem.setShopName(book.getShopName());
                    bookItem.setTotal(book.getPrice()*bookNum);
                    items.add(bookItem);
                }
                OrderList orderList = new OrderList();
                orderList.setBookList(items);
                orderList.setOrderState(Config.placeit);
                allOrder.getOrderMap().put(i,orderList);
            }
            return allOrder;
        }else {
            return null;
        }
    }
}
